#!/bin/bash

# 检查是否传入 clean 参数
if [ "$1" = "clean" ]; then
    echo "执行清理操作..."
    for dir in build install log; do
        if [ -d "$dir" ]; then
            echo "删除 $dir 目录..."
            rm -rf "$dir"
            echo "$dir 目录已删除"
        else
            echo "$dir 目录不存在，跳过删除"
        fi
    done
    echo "清理完成"
fi


source /opt/ros/humble/setup.bash

# 定义需要编译的包列表
PACKAGES=(
    # "cloud_service"
    # "custom_interface_pkg"
    # "audio_stream"
    # "insta360_camera"
    # "compressed_depth_image_transport"
    "multi_launch_demo"
)

# 如果传入了参数，则只编译指定的包
if [ $# -gt 0 ] && [ "$1" != "clean" ]; then
    # 重置包列表为传入的参数
    PACKAGES=("$@")
    # 如果第一个参数是 clean，则移除它
    if [ "$1" = "clean" ]; then
        shift
        PACKAGES=("$@")
    fi
fi


# 遍历包列表进行编译
for package in "${PACKAGES[@]}"; do
    echo "编译包: $package"
    if [ "$package" = "webrtc_server" ]; then
        # 为 webrtc_server 添加特殊的 cmake 参数
        colcon build --packages-select "$package" --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo -DEN_DEBUG=true -DEN_UNIT_TEST=true --event-handlers console_direct+
    else
        # 其他包使用默认参数
        colcon build --packages-select "$package" --event-handlers console_direct+
    fi
done